<html>
<head>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="crypto.js"></script>
</head>
<body>
<h1>示例页面</h1>
<button type="button" id="getToken">Token获取</button>
<button type="button" id="refreshToken">刷新Token</button>
<button type="button" id="btn">请求接口</button>

<div id="div1">
</div>

<script type="text/javascript">

var accessToken = "";
var refreshToken = "";


$("#getToken").click(function(){
	var url="http://127.0.0.1:82/api/getAccessToken";
	var data = {"account":"ceshi1", "password":"123"};
	var params = jsonSort(data);
    var signData = getSignature(params);
    $.ajax({
		type: "GET",
		url: url,
		headers:signData,
		data:data,
		dataType:"json",
		contentType: "application/x-www-form-urlencoded",
		success: function(res){
			$("#div1").html("请求结果："+JSON.stringify(res));
			accessToken = res.data.accessToken;
			refreshToken = res.data.refreshToken;
		}
	});
});

$("#refreshToken").click(function(){
	var url="http://127.0.0.1:82/api/refreshAccessToken";
	var data = {"memberId":"1", "refreshToken":refreshToken};
	var params = jsonSort(data);
    var signData = getSignature(params);
    $.ajax({
		type: "GET",
		url: url,
		headers:signData,
		data:data,
		dataType:"json",
		contentType: "application/x-www-form-urlencoded",
		success: function(res){
			$("#div1").html("请求结果："+JSON.stringify(res));
			accessToken = res.data.accessToken;
			refreshToken = res.data.refreshToken;
		}
	});
});

$("#btn").click(function(){
    var url="http://127.0.0.1:82/api/setPassword";
	var data = {"memberId":2, "password":"123"};
	var params = jsonSort(data);
	var signData = getSignature(params);
	signData.accessToken = accessToken;
    $.ajax({
		type: "POST",
		url: url,
		headers:signData,
		data:data,
		dataType:"json",
		contentType: "application/x-www-form-urlencoded",
		success: function(data){
			$("#div1").html("请求结果："+JSON.stringify(data));
		}
	});
});


	function randomNum(digit){
		var result=""; //定义变量并初始化
		if(digit=="" || isNaN(digit)){
			//alert("请输入数字");
		}else{
			var sourceStr="0,1,2,3,4,5,6,7,8,9";
			arrStr=sourceStr.split(",");
			
			var index=0;
			for(i=0;i<digit;i++){
				index=parseInt(Math.random()*arrStr.length);
				result+=arrStr[index];
			}
		}
		return result;
	}
	
	function jsonSort(jsonObj) {
		var arr = [];
		for (var key in jsonObj) {
			arr.push(key)
		}
		arr.sort();
        var str = '';
		for (var i in arr) {
			str += arr[i] + "=" + jsonObj[arr[i]] + "&"
		}
		return str.substr(0, str.length - 1)
	}


	function getSignature(params) {
        var publicKey="123";
        var apiVersion = "1.0";
        var clientOs = "iOS";
        var clientType = "HTML5";
        var timestamp = Date.parse(new Date());
        var nonce = randomNum(8);
        var signMethod = "HMAC-SHA1";
        var signData = {"Api-Version":apiVersion,"Client-OS":clientOs,"Client-Type":clientType,"TimeStamp":timestamp,"Nonce":nonce,"Sign-Method":signMethod};
        var signParams=jsonSort(signData);
        var encodeSignParams = encodeURIComponent(signParams+"&"+params);
        //使用算法 第一个参数为加密字符串，第二个参数为公共秘钥
        var sign=CryptoJS.HmacSHA1(encodeSignParams,publicKey+"&");
        var base64Sign = window.btoa(sign);
        signData.Sign = base64Sign;
        return signData
	}

</script>
</body>
</html>